package com.burton.dp.master;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class MasterDemo {

	static String path = "/master_path";
	static CuratorFramework client = CuratorFrameworkFactory.builder()
			.connectString("127.0.0.1:2181").sessionTimeoutMs(5000)
			.retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();

	
	public static void main(String[] args) throws Exception {
		client.start();

		LeaderSelector select = new LeaderSelector(client, path,
				new LeaderSelectorListenerAdapter() {

					public void takeLeadership(CuratorFramework client)
							throws Exception {
						System.out.println("成为master 角色");
						Thread.sleep(1000);
						System.out.println("释放master 角色");
					}
				});

		select.autoRequeue();
		select.start();
		Thread.sleep(Integer.MAX_VALUE);
	}

}
